<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="进货报表按供应商详情">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">
    <script src="${baseUrl}js/myEchart.js" charset="utf-8"></script>


    <style type="text/css">
        label{
            display:inline-block;
            width:80px;
            color: #999;
        }

        .info{
            color: #666;
            padding: 2px;
        }
        .layui-card {border:1px solid #f2f2f2;border-radius:5px;}
    </style>

    <script>
        jQuery(function () {

            $("#cancel").click(function () {
                closeSubIframe();
            });
        });

        layui.use(['form', 'table','element'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                element = layui.element,
                util = layui.util;

            renderOrderTable();
            renderGoodsTable();

            $("#search-order-btn").click(function () {
                table.reload("table_order", {where:genSearchOrder(),page: {curr: 1}});
            });

            $("#export-order").click(function () {
                exportOrderExcel();
            });

            $("#search-goods-btn").click(function () {
                table.reload("table_goods", {where:genSearchGoods(),page: {curr: 1}});
            });

            $("#export-goods").click(function () {
                exportGoodsExcel();
            });
        });


        function genSearchOrder() {
            let json = getFormJson($("#searchForm"));
            let data = {
                close:false,
                providerId:"${providerId}",
                storeId: "${storeId}",
                houseId: "${houseId}",
                date1: "${date1}",
                date2: "${date2}",
                search:json.search_order
            };
            return data;
        }

        function genSearchGoods() {
            let json = getFormJson($("#searchForm"));
            let data = {
                fileName:"进货报表按供应商按商品表",
                filterZero: true,
                providerId:"${providerId}",
                storeId: "${storeId}",
                houseId: "${houseId}",
                date1: "${date1}",
                date2: "${date2}",
                search:json.search_provider
            };

            return data;
        }

        function renderOrderTable() {
            layui.table.render({
                id:'table_order',
                elem: '#table_order',
                totalRow:true,
                url: "${baseUrl}goodsCount/inGoodsAnalysis/getOrderDetail.action",
                where : {
                    close:false,
                    providerId:"${providerId}",
                    storeId: "${storeId}",
                    houseId: "${houseId}",
                    date1: "${date1}",
                    date2: "${date2}",
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                            return d.LAY_INDEX;
                        }},
                    {field: 'date', width: 100, title: '日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(d.date);
                        }},
                    {field: 'no', width: 150, title: '单据编号',style:"font-size:12px;",templet:'#linkOrderBar'},
                    {width: 250, title: '进货商品',style:"font-size:12px;", templet: function(d){
                            return genGoodsAndPacket(d.inGoodsGoodsVoList, d.inGoodsPacketVoList);
                        }},
                    {field: 'oldTotalAmt', width: 130, title: '进货额',style:"font-size:12px;",templet: function (d) {
                            return getPrice(d.oldTotalAmt);
                        }},
                    {field: 'tax',width: 120, title: '税额',style:"font-size:12px;",templet: function (d) {
                            return getPrice(d.tax);
                        }},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    let number = 0;
                    let oldTotalAmt = 0;
                    let tax = 0;
                    $.each(res.data, function (i,d) {
                        number += parseFloat(d.number);
                        oldTotalAmt += parseFloat(d.oldTotalAmt);
                        tax += parseFloat(d.tax);
                    });

                    setTotalRowWithParent($('[lay-id="table_order"]'),"number",getNumber(number));
                    setTotalRowWithParent($('[lay-id="table_order"]'),"oldTotalAmt",getPrice(oldTotalAmt));
                    setTotalRowWithParent($('[lay-id="table_order"]'),"tax",getPrice(tax));
                    setTotalRowWithParent($('[lay-id="table_order"]'),"0","合计");

                    $(".linkNo").click(function () {
                        openTab($(this).attr("linkTitle"),$(this).attr("linkUrl"));
                    });
                }
            });
        }

        function renderGoodsTable() {
            layui.table.render({
                id:'table_goods',
                elem: '#table_goods',
                toolbar: '#tableToolBar',
                defaultToolbar:['filter'],
                totalRow:true,
                url: "${baseUrl}goodsCount/inGoodsAnalysis/listGoodsData.action",
                where : {
                    filterZero: true,
                    providerId:"${providerId}",
                    storeId: "${storeId}",
                    houseId: "${houseId}",
                    date1: "${date1}",
                    date2: "${date2}",
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                            return d.LAY_INDEX;
                        }},
                    {field: 'no', width: 150, title: '商品编号',style:"font-size:12px;"},
                    {field: 'name',width: 250, title: '商品名称',style:"font-size:12px;"},
                    {field: 'norms',width: 120, title: '规格',style:"font-size:12px;"},
                    {field: 'unitName',width: 120, title: '单位',style:"font-size:12px;"},

                    {field: 'number',width: 120, title: '进货量',style:"font-size:12px;"},
                    {field: 'amount',width: 120, title: '进货额',style:"font-size:12px;",templet: function (d) {
                            return getPrice(d.amount);
                        }},
                    {field: 'avgAmount',width: 120, title: '进货均价',style:"font-size:12px;",templet: function (d) {
                            return getPrice(d.avgAmount);
                        }},
                    {field: 'tax',width: 120, title: '税额',style:"font-size:12px;",templet: function (d) {
                            return getPrice(d.tax);
                        }},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    let number = 0;
                    let amount = 0;
                    let tax = 0;
                    $.each(res.data, function (i,d) {
                        number += parseFloat(d.number);
                        amount += parseFloat(d.amount);
                        tax += parseFloat(d.tax);
                    });
                    setTotalRowWithParent($('[lay-id="table_goods"]'),"number",getNumber(number));
                    setTotalRowWithParent($('[lay-id="table_goods"]'),"amount",getPrice(amount));
                    setTotalRowWithParent($('[lay-id="table_goods"]'),"tax",getPrice(tax));
                    setTotalRowWithParent($('[lay-id="table_goods"]'),"0","合计");
                }
            });
        }

        function exportOrderExcel() {
            confirm("是否导出所有的数据?",function () {
                tipLoad("处理中，请稍后");
                let search = $("#search").val();
                post("${baseUrl}goodsCount/inGoodsAnalysis/exportProviderDetailDataByOrder.action" + genParam(genSearchOrder()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function exportGoodsExcel() {
            confirm("是否导出所有的数据?",function () {
                tipLoad("处理中，请稍后");
                post("${baseUrl}goodsCount/inGoodsAnalysis/exportGoodsData.action" + genParam(genSearchGoods()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        errTip(resp.msg);
                    }
                });
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-card">
            <div class="layui-card-header">
                <div class="font16 fontB fontDeepBlue">
                    ${provider.name}
                    <span class="pdl4 pdr4">-</span>
                    进货明细
                </div>
            </div>
            <div class="layui-card-body">
                <div class="info"><label>时间周期:</label>
                    <c:choose>
                        <c:when test="${not empty date1 and not empty date2}">
                            ${date1}
                            <span style="padding:0px 4px 0px 4px;color: #c2c2c2">~</span>
                            ${date2}
                        </c:when>

                        <c:otherwise>
                            全部
                        </c:otherwise>
                    </c:choose>
                </div>
                <div class="info"><label>门店仓库:</label>
                    <c:choose>
                        <c:when test="${empty storeId}">
                            全部
                        </c:when>

                        <c:when test="${empty houseId}">
                            ${storeName}

                            <span style="padding:0px 4px 0px 4px;color: #c2c2c2"> &gt; </span>
                            全部
                        </c:when>

                        <c:otherwise>
                            ${storeName}

                            <span style="padding:0px 4px 0px 4px;color: #c2c2c2"> &gt; </span>
                            ${houseName}
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </div>

        <div>
            <div class="layui-card">
                <div class="layui-card-header">进货趋势</div>
                <div class="layui-card-body">
                    <div id="trend" class="h200"></div>
                    <div class="h200 lh200 font20 fontB fontC_ccc" style="text-align: center;display: none;">
                        统计值为0或负数无法统计
                    </div>
                </div>
            </div>
            <script>
                function genSearch() {
                    let data = {
                        providerId:"${providerId}",
                        storeId: "${storeId}",
                        houseId: "${houseId}",
                        date1: "${date1}",
                        date2: "${date2}",
                    };
                    return data;
                }

                layui.use(['echarts'], function () {
                    let $ = layui.jquery,
                        echarts = layui.echarts;
                    trend_chat = echarts.init(document.getElementById('trend'),'walden');
                    window.onresize = function(){
                        trend_chat.resize();
                    };
                    get("${baseUrl}goodsCount/inGoodsAnalysis/chatDetailData.action"+genParam(genSearch()), function (res) {
                        if (!emptyEntity(res.data)) {
                            $("#trend").css("display","block");
                            $("#trend").next().css("display","none");
                            let dateList = [];
                            let amountList = [];
                            for (let key in res.data) {
                                dateList.push(key);
                                amountList.push(getPrice(res.data[key]));
                            }

                            trend_chat.setOption(genChatLineOption("退货额",dateList,amountList));
                        }else{
                            $("#trend").css("display","none");
                            $("#trend").next().css("display","block");
                        }
                    });
                });

            </script>
        </div>

        <form id="searchForm" class="layui-form">
            <div class="layui-tab layui-tab-brief" lay-filter="total-data">
                <ul class="layui-tab-title">
                    <li class="layui-this">按单据</li>
                    <li>按商品</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show" style="min-height: 200px;">
                        <div class="layui-input-inline">
                            <input type="text" name="search_order" placeholder="搜索编号、商品名称" class="layui-input" autocomplete="off" style="font-size: 12px;">
                        </div>

                        <div class="layui-inline">
                            <a class="layui-btn" id="search-order-btn"><i class="layui-icon layui-icon-search"></i></a>
                            <a class="layui-btn layui-btn-primary" id="export-order">导出</a>
                        </div>
                        <table class="layui-hide" id="table_order" lay-filter="table_order" lay-size="sm"></table>
                    </div>


                    <div class="layui-tab-item" style="min-height: 200px;">
                        <div class="layui-input-inline">
                            <input type="text" name="search_provider" placeholder="搜索编号、名称、规格" class="layui-input" autocomplete="off" style="font-size: 12px;">
                        </div>

                        <div class="layui-inline">
                            <a class="layui-btn" id="search-goods-btn"><i class="layui-icon layui-icon-search"></i></a>
                            <a class="layui-btn layui-btn-primary" id="export-goods">导出</a>
                        </div>
                        <table class="layui-hide" id="table_goods" lay-filter="table_goods" lay-size="sm"></table>
                    </div>
                </div>
            </div>
        </form>

        <script type="text/html" id="linkOrderBar">
            {{#  if((_login_user.viewOtherOrder && _login_user.store.sys) || _login_user.id==d.userId){ }}
            <a class="linkNo" href="javascript:;" linkUrl="${baseUrl}inGoods/inGoodsDetail.action?id={{d.id}}" linkTitle="进货单详情" >{{d.no}}</a>
            {{# }else{ }}
            {{d.no}}
            {{# } }}
        </script>
    </div>
</div>
</body>
<html:loading/>
</html>